package tr.edu.metu.is.mega.server.business;

import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.common.dto.LectureNoteDTO;
import tr.edu.metu.is.mega.server.core.AbstractDTOHelper;
import tr.edu.metu.is.mega.server.dao.CourseDAO;
import tr.edu.metu.is.mega.server.entity.Lecturenote;

/**
 * Helper class to convert between DTO and entity.
 */
public class LectureNoteDTOHelper extends AbstractDTOHelper<LectureNoteDTO, Lecturenote> {

    private final CourseDAO courseDAO;

    /**
     * Default constructor.
     */
    public LectureNoteDTOHelper() {
        courseDAO = new CourseDAO();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public LectureNoteDTO convertToDTO(Lecturenote from) {
        LectureNoteDTO to = new LectureNoteDTO();
        to.setPkid(from.getPkid());
        to.setDescription(from.getDescription());
        to.setFilePath(from.getFilepath());
        CourseDTO courseDTO = new CourseDTO();
        courseDTO.setPkid(from.getCourse().getPkid());
        courseDTO.setCode(from.getCourse().getCode());
        to.setCourse(courseDTO);

        return to;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void convertToEntity(LectureNoteDTO from, Lecturenote to) {
        to.setPkid(from.getPkid());
        to.setDescription(from.getDescription());
        to.setFilepath(from.getFilePath());
        to.setCourse(courseDAO.findById(from.getCourse().getPkid()));
    }

}
